package com.ojoin.ojoin.eb_spider.queue;

import com.ojoin.ojoin.eb_spider.bean.TJUserOrder;
import com.ojoin.ojoin.eb_spider.service.StatTotalService;
import com.ojoin.ojoin.eb_spider.service.impl.SLDataCatchBaseServiceImpl;
import com.ojoin.ojoin.eb_spider.utils.SpringUtilsSpider;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.json.JSONUtils;
import com.ojoin.trade.supplyloan_core.web.domain.Order;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.jms.*;
import java.util.UUID;

/**
 * 
 * @author liang
 * @description 队列消息监听器
 * 
 */
@Scope("prototype")
@Component
public abstract class BaseReceiverListener implements MessageListener {

	private static Logger log = LoggerFactory
			.getLogger(BaseReceiverListener.class);
	/**
	 * 接听器名称
	 */
	private String name = UUID.randomUUID().toString();

	@Override
	public void onMessage(Message message) {
		try {
			LogUtils.info(log, " comsumer{%s}recevies message %s", name,
					message.toString());

			if (message instanceof ObjectMessage) {
                             LogUtils.info(log,"receive ObjectMessage",message);

			}else if(message instanceof TextMessage){
				LogUtils.info(log,"receive TextMessage",message);

				TextMessage om = (TextMessage) message;
				String ob = om.getText();

				TJUserOrder to = JSONUtils.parseObject(ob,TJUserOrder.class);

				deal(ob);
			}else {
				LogUtils.info(log,"message instanceof ObjectMessage is false ");
			}
		} catch (JMSException e) { 
			LogUtils.error(log, e);
		} catch (BusinessException e) {
			LogUtils.error(log, e);
		} catch (Exception e) {
			LogUtils.error(log, e);
		}

	}

	abstract void deal(Object ob) throws Exception;

}
